package main

import (
	"fmt"

	//"math/rand"
	//"time"
	//"encoding/json"
	//	"initRoute"
	//	"controller"
	//	"encoding/json"
	//"net/http"
	"cmsapi/src/lib"
	//	"reflect"
	"cmsapi/src/service"

	"github.com/gin-gonic/gin"
)

func main() {
	config := lib.Config()

	//fmt.Println(config)
	/*
		s := `{"aa":"bb","cc":{"d1":"d2"}}`
		var reply map[string]interface{}

		json.Unmarshal([]byte(s), &reply)
		fmt.Println("reply:", reply, "\r\n")
		for i, num := range reply {
			fmt.Println("index:", i, num)
		}
	*/
	//aa := (*reply)["aa"]
	//fmt.Println("----", (reply).rang, aa)
	/*
		//var greeting interface{}
		//s.(interface{})

		switch g := reply.(type) {
		case string:
			fmt.Println("g is a string with length", len(g))
		case int:
			fmt.Println("g is an integer, whose value is", g)
		case map:
			fmt.Println("map", g)
		default:
			fmt.Printf("other %s=>%T", g, g)
		}
	*/

	/*
		t := map[string]string{"a": "b"}
		var reply map[string]string
		dataType, _ := json.Marshal(t)
		data := string(dataType)
		json.Unmarshal([]byte(data), &reply)

		fmt.Printf("t4 %s=>%T | %s=>%T  %s  \r\n", t, t, reply, reply, reply["a"])
	*/
	//	s:= Init();
	//	v:=controller.Init()
	version := "v1"
	fmt.Println("start")
	// get,_:=initRoute.Init()
	router := gin.Default()
	/*
	   	router.GET(version+"/home", func(c *gin.Context) {
	   		//_get:=c.Query();//c.Request.URL.Query();
	   		c.String(http.StatusOK,"首页=>%s",c.Query("name"))
	   	//	firstname := context.DefaultQuery("firstname", "Guest")
	   	//	lastname := context.Query("lastname") // 是 c.Request.URL.Query().Get("lastname") 的简写
	   id
	   	})
	*/
	//router.GET("/test", MyMiddleware, service.Json)
	home := router.Group("/" + version + "/home")
	{
		//home.Use(service.AllGet)
		//	home.GET("/:controller", service.Request)
		//	home.GET("/get/:action", service.Request)
		//router.GET("/test", MyMiddleware, service.Json)
		//自定义中间件 + 自定义上下文
		home.GET("/login", service.Login, lib.Json)
		home.Use(lib.Check())
		{
			//	home.GET("/login2", service.One2, lib.Json)

			home.GET("/one/:action", service.One, lib.Json)
			home.GET("/list/:action", service.List, lib.Json)
			home.POST("/save/:action", service.Save, lib.Json)
		}
		/*
			home.GET("", func(c *gin.Context) {
				c.String(http.StatusOK, "首页=>%s", c.Query("name"))
			})
			home.GET("/:id", func(c *gin.Context) {
				c.String(http.StatusOK, "ID=>%s", c.Param("id"))
			})

			home.GET("/activity/index", service.ActivityIndex)
			home.Use(service.Check())
			{
				home.GET("/activity/detail", service.ActivityDetail)
				//home.GET("/activity/detail2", service.ActivityDetail2)
				home.POST("/activity/save", service.ActivitySave)
				home.DELETE("/activity/del", service.ActivityDel)

				home.GET("/activity/de", service.ActGormDetail)
			}

		*/
	}
	router.GET("/activity/de", service.ActGormDetail)
	fmt.Println("end")
	//router.Run(":9001")
	router.Run(((config["server"]["Domain"]).(string)) + ":" + ((config["server"]["Prot"]).(string)))
}
